import axios, { AxiosRequestConfig } from 'axios'
//import { API_URL, LOGIN_TOKEN_KEY } from '../config'

export interface Options {
    method : 'POST' | 'GET' | 'DELETE' | 'PUT';
    data? : any;
    params? : object;
}

export interface Result {
    success : boolean;
    code? : number;
    data? : any;
    msg : string;
}

class Http {
    private sending:boolean = false;

    async post<T = Result>(url: string, data?: any, params? : object)  : Promise<T>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.post<T>(url, data, params)
        this.sending = false
        return result
    }

    static post<T = Result>(url: string, data?: any, params? : object) : Promise<T> {
        let options:Options = {
            method : 'POST'
        }
        if(data) {
            options.data = data
            // if(data instanceof FormData) {
            //     options.processData = false
            // }
        }

        if(params) {
            options.params = params
        }

        return Http.send<T>(url, options)
    }

    async get<T = Result>(url: string, params? : object, data?: any) : Promise<T>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.get<T>(url, params, data)
        this.sending = false
        return result
    }

    static get<T = Result>(url: string, params? : object, data?: any) : Promise<T>{
        let options:Options = {
            method : 'GET'
        }

        if(params) {
            options.params = params
        }

        if(data) {
            options.data = data
        }

        return Http.send<T>(url, options)
    }

    async put<T = Result>(url: string, data?: any, params? : object)  : Promise<T>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.put<T>(url, data, params)
        this.sending = false
        return result
    }

    static put<T = Result>(url: string, data?: any, params? : object) : Promise<T> {
        let options:Options = {
            method : 'PUT'
        }
        if(data) {
            options.data = data
            // if(data instanceof FormData) {
            //     options.processData = false
            // }
        }

        if(params) {
            options.params = params
        }

        return Http.send<T>(url, options)
    }

    async delete<T = Result>(url: string, data?: any, params? : object) : Promise<T>{
        if(this.sending) {
            throw '请等待'
        }
        this.sending = true
        let result = await Http.delete<T>(url, data, params)
        this.sending = false
        return result
    }

    static delete<T = Result>(url: string, data?: any, params? : object) : Promise<T>{
        let options:Options = {
            method : 'DELETE'
        }

        if(params) {
            options.params = params
        }

        if(data) {
            options.data = data
        }

        return Http.send<T>(url, options)
    }

    static async send<T>(url: string, options: Options) : Promise<T>{
        try {

            let config:AxiosRequestConfig = {
                //baseURL : API_URL,
                url : 'http://api.newbeedata.com/api/' + url,
                ...options,
                responseType : 'json',
                headers : {
                    
                }
            }

            // let token = localStorage.getItem(LOGIN_TOKEN_KEY)
            // if(token) {
            //     config.headers.Authorization = token
            // }
            

            let response = await axios(config)

            return response.data
           
        } catch (error:any) {
            return {
                success : false,
                code : 404,
                message : error.message
            } as any
        }
    }


    private static _change(data : any) {
        // if(typeof data !== 'object') return

        // for(let k in data) {
        //     let keyType = typeof k
        //     let valueType = typeof data[k]
        //     if(keyType === 'string') { 
        //         if(k.includes('_')) { //需要转驼峰
        //             let tmpValue = cloneDeep(data[k])
        //             delete data[k]
        //             k = Http._hump(k)
        //             data[k] = tmpValue
        //         }
        //     }

        //     if(valueType === 'object') {
        //         Http._change(data[k])
        //     }
        // }

    }

    private static _hump(name: string) {
        name = name.replace(/_(\w)/g, function($0, $1) {
            return $1.toUpperCase();
        })

        return name
    }
}

export default Http